from rest_framework.generics import ListAPIView
from rest_framework_extensions.cache.mixins import ListCacheResponseMixin
from areas.models import Area
from areas.serializers import AreaSerializer


class AreaView(ListAPIView):
    """
    当访问该页面时，请求省级地址数据返回
    :param request:
    :return:
    """
    serializer_class = AreaSerializer
    queryset = Area.objects.filter(parent=None)
    # def get(self,request):
    #     return Response({"error":"未完善的area主页数据"})


# class AreaSecondView(ListAPIView):
#     serializer_class = AreaSerializer
#
#     def get(self):
#         # data = Area.objects.filter(parent=mobile)
#         # print(data.parent[1])
#         # data = data.parent.parent
#
#         return Area.objects.filter(parent_id=A())
class AreaSecondView(ListCacheResponseMixin,ListAPIView):
    """
        获得　市和区县一级信息，返回列表信息
        所有特殊专一类，都必须指定序列化器serializer_class，查询集queryset
        当正常方法无法得到serializwe_class或queryset时，可以从专一类的父类中寻找得到
        该serializwe_class或queryset的类方法，然后将其重写，返回需要的需求
    """
    serializer_class = AreaSerializer
    def get_queryset(self):
        """
          url(r'^areas/(?P<pk>\d+)/$', views.AreaSecondView.as_view()),
          as_view中的获得字典形式　{pk:120000}
        :return:
        """
        # pk=self.args.get
        # if pk==None:
        pk=self.kwargs['pk']
        return Area.objects.filter(parent_id=pk)





